<template>
  <van-button
    :icon="isliked===1 ? 'good-job' : 'good-job-o'"
    :class="{
      'like-article': isliked===1
    }"
    :loading="loading"
    @click="onCollect"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  name: 'likeArticle',
  components: {},
  props: {
    isliked: {
      // type: Number,
      required: true
    },
    articleId: {
      type: [String, Number, Object],
      required: true
    }
  },
  data () {
    return {
      isCollect: false,
      loading: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onCollect () {
      this.loading = true
      // 这里 loading 不仅仅是为了交互提示，更重要的是请求期间禁用背景点击功能，防止用户不断的操作界面发出请求
      //   this.$toast.loading({
      //     duration: 0, // 持续展示 toast
      //     message: '操作中...',
      //     forbidClick: true // 是否禁止背景点击
      //   })
      try {
        let status = -1
        if (this.isliked === 1) {
          await deleteLike(this.articleId)
        } else {
          await addLike(this.articleId)
          status = 1
        }
        this.$emit('update-is_liked', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast.fail('操作失败')
      }
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
    .like-article{
        .van-icon{
            color: #e5645f
        }
    }
</style>
